package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.CRMFilterFirstData;
import com.lzx.iteam.bean.CRMFilterSecondData;
import com.lzx.iteam.bean.aidl.ChatMoreMsg;

import java.util.ArrayList;

/**
 * Created by Megan on 16/7/28.
 */
public class CRMFilterAdapter extends BaseAdapter {

    private ArrayList<Object> mFilterDatas;
    private Context mContext;
    private LayoutInflater mInflater;
    private int mType = 0;//0:一级目录  1:2级目录
    private int mPosition=0;

    public CRMFilterAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        mFilterDatas = new ArrayList<Object>();
    }

    public void bindData(int type, ArrayList<Object> filterDatas) {
        mFilterDatas.clear();
        mFilterDatas.addAll(filterDatas);
        mType = type;
        notifyDataSetChanged();
    }

    public void setSelectedPosition(int position){
        this.mPosition=position;
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return mFilterDatas == null ? 0 : mFilterDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mFilterDatas == null ? null : mFilterDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            if (mType == 0) {//一级筛选
                convertView = mInflater.inflate(R.layout.item_crm_filter_first, parent, false);
                viewHolder.text = (TextView) convertView.findViewById(R.id.tv_item_crm_filter_first);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.iv_item_crm_filter_first);
                convertView.setTag(viewHolder);
            } else {//二级筛选
                convertView = mInflater.inflate(R.layout.item_crm_filter_second, parent, false);
                viewHolder.text = (TextView) convertView.findViewById(R.id.tv_item_crm_filter_second);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.iv_item_crm_filter_second);
                viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_item_crm_filter_second);
                convertView.setTag(viewHolder);
            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mType == 0) {
            if(mPosition==position){
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.crm_filter_bg1));
            }else{
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.white));

            }
            CRMFilterFirstData firstData = (CRMFilterFirstData) mFilterDatas.get(position);
            viewHolder.text.setText(firstData.getName());
            if (firstData.getState() == 1) {
                viewHolder.image.setVisibility(View.VISIBLE);
            } else {
                viewHolder.image.setVisibility(View.GONE);
            }
        } else if (mType == 1) {
            CRMFilterSecondData secondData = (CRMFilterSecondData) mFilterDatas.get(position);
            viewHolder.text.setText(secondData.getName());
            if (secondData.getType() == 1) {
                viewHolder.image.setVisibility(View.GONE);
                viewHolder.checkBox.setVisibility(View.VISIBLE);
                if(secondData.getState()==1){
                    viewHolder.checkBox.setChecked(true);
                }else{
                    viewHolder.checkBox.setChecked(false);
                }
            } else {
                viewHolder.checkBox.setVisibility(View.GONE);
                if (secondData.getState() == 1) {
                    viewHolder.image.setVisibility(View.VISIBLE);

                } else {
                    viewHolder.image.setVisibility(View.GONE);

                }
            }

        }


        return convertView;
    }

    class ViewHolder {
        private TextView text;
        private ImageView image;
        private CheckBox checkBox;
    }
}
